<?PHP

class DataScraperToWordPress{
	
	public static function inputDataIntoDatabase($array_data_retrieved)
	{
		$logs = array();
		$database_links_exist = false;
		$url_check_arrays = DataScraper::searchForLinks($array_data_retrieved);
		//$url_check_arrays = DataScraper::searchForLinks(Database::Select(CONF::$WPPrefex."dataset", "*", array("wp_post_ID"=>Database::Select(CONF::$WPPrefex."posts", "id", array("AND"=>array("post_status"=>"publish", "post_type"=>"dataset"))))));
		
		/*$database_url_check_array = array();
		$database_array = Database::Select(CONF::$WPPrefex."dataset", "*", array("wp_post_ID"=>Database::Select(CONF::$WPPrefex."posts", "id", array("AND"=>array("post_status"=>"publish", "post_type"=>"dataset")))));
		if($database_array != false && $database_array != null)
			addToArray($database_url_check_array, DataScraper::searchForLinks($database_array));
		$database_array =  Database::Select(CONF::$WPPrefex."posts", "*", array("AND"=>array("post_status"=>"publish", "post_type"=>"dataset")));
		if($database_array != false && $database_array != null)
			addToArray($database_url_check_array, DataScraper::searchForLinks($database_array));
		*/	
		//used check for doubles in system for instance where data already exists
		//$count_of_urls = array_count_values($url_check_arrays);
		$array_urls_added = array();
		for($i = 0; $i < count($array_data_retrieved); $i++)// as $dataRetrieved)
		{
			echo "<br><br>$i/".count($array_data_retrieved)."<br>";
			$dataRetrieved = $array_data_retrieved[$i];
			$dataRetrieved_urls = DataScraper::searchForLinks(array($dataRetrieved[0]));
			//foreach($dataRetrieved_urls as $urls_row)
			if(!checkIfExists($array_urls_added, $dataRetrieved[0]["dataset_url"]))//$count_of_urls[$dataRetrieved[0]["dataset_url"]] == 1
			{
				$dataRetrieved_dataset_url = $dataRetrieved[0]["dataset_url"];
				addToArray($array_urls_added, $dataRetrieved_dataset_url);
				$url_exists = Database::Select(CONF::$WPPrefex."dataset", "*", array(
					"AND"=>
						array(
							"url" => $dataRetrieved_dataset_url
							,"wp_post_ID" => Database::Select(CONF::$WPPrefex."posts", "id", array("AND"=>array("post_status"=>"publish", "post_type"=>"dataset")))
						)
				));
				array_push($logs, self::insertData($dataRetrieved, $url_exists) > 0 ? "Added/Updated Url: ".$dataRetrieved_dataset_url : "Failed Url: ".$dataRetrieved_dataset_url);
			}
			else
			{
				array_push($logs, "Failed Url(already added):".$dataRetrieved[0]["dataset_url"]);
				continue;
			}

		}
		return $logs;
	}
	
	
	private static function insertData($dataRetrieved, $updateData = false)
	{	
		$isUpdate = false;
		if($updateData != false)
		{
			$updateData = $updateData[0];
			$isUpdate = true;
		}
		$organizedDataSet = NULL;
		$organizedPostData = NULL;
		$category_term = array();
		$currentDate = date("Y-m-d H:i:s");
		date_default_timezone_set("UTC");
		$gmt_currentDate = date("Y-m-d H:i:s", time());
		$post_id = 0;
		$blogpost_id = 0;
		if($updateData != false)
		{
			if(count($updateData) != 0)
			{
				$extraDetails = "";
				//TODO setup data for insertion - $dataRetrieved
				$post_id = $updateData["wp_post_ID"];
				$blogpost_id = $updateData["dataset_id"];
	$wordpress_post_id = $updateData["wp_post_ID"];
				$updatePost = Database::Select(CONF::$WPPrefex."posts", "*", array("AND"=>array("post_status"=>"publish", "post_type"=>"dataset", "ID"=>$wordpress_post_id)));
	$updatePost = $updatePost[0];
				$data = $dataRetrieved[0];
				if($data["blogTitle"] != $updatePost["post_title"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Title<i>\n" . $data["blogTitle"] . "\n\n";
					$data["blogTitle"] = $updatePost["post_title"];
				}
				$post_content_check = explode("<br><br><!-- DATA EXTRACTION POINT: DO NOT EDIT THIS OR FURTHER THAN THIS, IT WILL BE REPLACED --><br><br><b>The Following Data Has Been Gathered From The Dataset Automatically: </b><br><br><br>", $updatePost["post_content"]);
				if(count($post_content_check) != 1)
				{
					$updatePost["post_content"] = $post_content_check[0];
				}
				if($data["blogContent"] != $updatePost["post_content"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Content<i>\n" . $data["blogContent"] . "\n\n";
					$data["blogContent"] = $updatePost["post_content"];
				}
				if($data["blogExcerpt"] != $updatePost["post_excerpt"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Excerpt<i>\n" . $data["blogExcerpt"] . "\n\n";
					$data["blogExcerpt"] = $updatePost["post_excerpt"];
				}
				if($data["organizationName"] != $updateData["name"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Owners Name<i>\n" . $data["organizationName"] . "\n\n";
					$data["organizationName"] = $updateData["name"];
				}
				if($data["organizationWebsite"] != $updateData["website"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Owners Website<i>\n" . $data["organizationWebsite"] . "\n\n";
					$data["organizationWebsite"] = $updateData["website"];
				}
				if($data["organizationDescription"] != $updateData["discription"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Owners  Description<i>\n" . $data["organizationDescription"] . "\n\n";
					$data["organizationDescription"] = $updateData["discription"];
				}
				if($data["organizationContactPerson"] != $updateData["contactpersoon"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Owners Contact Person<i>\n" . $data["organizationContactPerson"] . "\n\n";
					$data["organizationContactPerson"] = $updateData["contactpersoon"];
				}
				if($data["organizationEmail"] != $updateData["email"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Owners Email<i>\n" . $data["organizationEmail"] . "\n\n";
					$data["organizationEmail"] = $updateData["email"];
				}
				if($data["organizationPhoneNumber"] != $updateData["phone"])
				{
					$extraDetails = $extraDetails . "<i>Dataset Owners Phone Number<i>\n" . $data[""] . "\n\n";
					$data["organizationPhoneNumber"] = $updateData["phone"];
				}
				if($extraDetails != "")
				{
					$extraDetails = "<br><br><!-- DATA EXTRACTION POINT: DO NOT EDIT THIS OR FURTHER THAN THIS, IT WILL BE REPLACED --><br><br><b>The Following Data Has Been Gathered From The Dataset Automatically: </b><br><br><br>".$extraDetails;
					$data["blogContent"] = $data["blogContent"] . $extraDetails;
					$dataRetrieved[0] = $data;
				}
			}
			
		}
		else
		{
			$blogpost_id = Database::getDatabase()->max(CONF::$WPPrefex."dataset", "dataset_id") + 1;
			$post_id = Database::getDatabase()->max(CONF::$WPPrefex."posts", "ID") + 1;
		}
		$row = $dataRetrieved[0];
		if(DataScraper::validateDataGathered($row))
		{
			if($row["dataset_categories"] != NULL || $row["dataset_tags"] != NULL)
			{
				//taxonomy = category & dataset_type
				foreach($row["file_types"] as $tags)
				{
					array_push($category_term, array(
							"post"=>$post_id,
							"category"=>$tags,
							"type"=>"category"
						)	
					);
				}
				foreach($row["dataset_categories"] as $tags)
				{
					array_push($category_term, array(
							"post"=>$post_id,
							"category"=>$tags,
							"type"=>"category"
						)	
					);
				}
				foreach($row["dataset_tags"] as $tags)
				{
					array_push($category_term, array(
							"post"=>$post_id,
							"category"=>$tags,
							"type"=>"post_tag"
						)	
					);
				}
			}
			//create dataset post ready for addition to database
			$organizedPostData = array(
				"ID" => $post_id
				,"post_author" => 1
				,"post_date" => $currentDate
				,"post_date_gmt" => $gmt_currentDate
				,"post_content" => $row["blogContent"]
				,"post_title" => $row["blogTitle"]
				,"post_excerpt" => $row["blogExcerpt"]
				,"post_status" => "publish"
				,"comment_status" => "open"
				,"ping_status" => "open"
				,"post_name" => $row["blogTitle"] != "" ? strtolower(str_replace(" ", "-", $row["blogTitle"])) : ""
				,"post_modified" => $currentDate
				,"post_modified_gmt" => $gmt_currentDate
				,"post_content_filtered" => ""
				,"post_parent" => 0
				,"guid" => createGUID()
				,"menu_order" => 0
				,"post_type" => "dataset"
				,"comment_count" => 0
			);
			
			//prepare dataset metadata for database
			//find records database value
			$records = $row["records"];
			if($records == "" || $records == 0 )
			{
				$records = "undefined";
			}
			else
			{
				if(gettype($records) == "string")
				{
					$records = ($records == "1") ? "1 - 100 Rows" : ($records == "2") ? "100 - 1,000 Rows" : ($records == "3") ? "1,000 - 10,000" : ($records == "4") ? "10,000 - 100,000 Rows" : "more than 100,000 Rows";
				}
				else
				{
					$records = ($records > 0 && $records < 100) ? "1 - 100 Rows" : ($records < 1000) ? "100 - 1,000 Rows" : ($records < 10000) ? "1,000 - 10,000" : ($records < 100000) ? "10,000 - 100,000 Rows" : "more than 100,000 Rows";
				}
				
			}
			
			
			//find dataset type database value
			$datasetType = $row["dataset_type"];
			if($datasetType > 3 && $datasetType < 1 || gettype($datasetType) != "integer")
			{
				$datasetType = "other";
			}
			else
			{
				$datasetType = ($datasetType == 1) ? "downloadable file" : ($datasetType == 2) ? "queryble database" : ($datasetType == 3) ? "api" : "other";
			}
			
			//find filesize database value
			$size = $row["size"];
			if($size == 0 || $size == "")
			{
				$size = "undefined";
			}
			else
			{
				if(gettype($size) == "string")
				{
					$size = ($size == "1") ? "0 - 1 KB" : ($size == "2") ? "1 - 100 KB" : ($size == "3") ? "100 KB - 10 MB" : ($size == "4") ? "10 MB - 1 GB" : "more than 1 GB";
				}
				else
				{
					$size = ($size > 0 && $size < 1000) ? "0 - 1 KB" : ($size < 100000) ? "1 - 100 KB" : ($size < 10000000) ? "100 KB - 10 MB" : ($size < 1000000000) ? "10 MB - 1 GB" : "more than 1 GB";
				}
			}
			
			//find update frequency value
			$datasetUpdateTime = $row["dataset_update_time"];
			if($datasetUpdateTime > 6 && $datasetUpdateTime < 1)
			{
				$datasetUpdateTime = "realtime";
			}
			else
			{
				$datasetUpdateTime = ($datasetUpdateTime == 1) ? "realtime" : ($datasetUpdateTime == 2) ? "daily" : ($datasetUpdateTime == 3) ? "weekly" : ($datasetUpdateTime == 4) ? "monthly" :($datasetUpdateTime == 5) ? "annually" : ($datasetUpdateTime == 3) ? "static" : "realtime";
			}
	
			//organize data for dataset metadata for database
			//variables to fix = size, row
			$organizedDataSet = array(
				"dataset_id" => $blogpost_id
				,"wp_post_ID" => $post_id
				,"url" => $row["dataset_url"]
				,"general_technical_format" => NULL
				,"size" => NULL//$row["size"] == 1) ? "0-1KB" 
				,"records" => $records 
				,"protocol" => $row["protocol"]
				,"name" => $row["organizationName"]
				,"website" => $row["organizationWebsite"]
				,"discription" => $row["organizationDescription"]
				,"contactpersoon" => $row["organizationContactPerson"]
				,"email" => $row["organizationEmail"]
				,"phone" => $row["organizationPhoneNumber"]
				,"account" => 0
				,"update_frec" => $datasetUpdateTime
				,"details" => $row["extraMetadataDetails"]
				,"type" => $datasetType
				,"demo_url" => $row["dataset_demo_url"]
				,"screenshot" => ""//$row["dataset_screenshot"][0]//TODO SCREENSHOTS
			);
		}
		
		//add newly created fields into database
		$data_post_rows = NULL;
		$data_dataset_rows = NULL;
		$data_terms = NULL;
		$data_terms_rel = NULL;
		$data_terms_taxo = NULL;
		if($organizedPostData != NULL)
		{
			if($isUpdate != false)
	{
	$new_id = $organizedPostData["ID"];
				$data_post_rows = (int)Database::Update(CONF::$WPPrefex."posts", $organizedPostData, array("ID"=>$new_id));
	}
			else
				$data_post_rows = (int)Database::Insert(CONF::$WPPrefex."posts", $organizedPostData);
	
			if($data_post_rows)
			{
				if($isUpdate != false)
	{
	$new_id = $organizedPostData["ID"];
					$data_dataset_rows = (int)Database::Update(CONF::$WPPrefex."dataset", $organizedDataSet,  array("dataset_id"=>$new_id));
	}
				else
					$data_dataset_rows = (int)Database::Insert(CONF::$WPPrefex."dataset", $organizedDataSet);
			}
		}
		if($data_dataset_rows || $isUpdate != false)
		{
			if(count($category_term) > 0)
			{	
				$terms = Database::Select(CONF::$WPPrefex."terms", "*");
				//$taxo = Database::Select(CONF::$WPPrefex."term_taxonomy", "*");
				$relationships = Database::Select(CONF::$WPPrefex."term_relationships", "*");
				foreach($category_term as $term)
				{
					//			"post"=>$post_id,
					//			"category"=>$tags,
					//			"type"=>"category"
					$termexists = false;
					$taxoexists = false;
	$term_category = $term["category"];
	$term_type = $term["type"];
					$termdata = Database::Select(CONF::$WPPrefex."term_taxonomy", "*", array(
						"AND" => array(
							"term_id" => Database::Select(CONF::$WPPrefex."terms", "term_id", array("slug" => self::slug($term_category) )),
							"taxonomy" => $term_type
								)
					));
					if(count($termdata) == 0 || $termdata == false)
					{
						$data = checkIfExists($terms, self::slug($term_category), "slug", true);
						if($data == false)
							$data = Database::Insert(CONF::$WPPrefex."terms", array(
								"name"=>$term_category,
								"slug"=>strtolower(str_replace(" ", "-",$term_category))
							));
						else
							$data = $data[0]["term_id"];
						$termdata = Database::Insert(CONF::$WPPrefex."term_taxonomy", array(
							"term_id"=> (int)$data,
							"taxonomy"=>$term_type
						));
					}
					if(gettype($termdata) == "array")
					{
						$termdata = $termdata[0]["term_taxonomy_id"];
					}
					if(checkIfExists(checkIfExists($relationships, (string)$termdata, "term_taxonomy_id", true), $term["post"], "object_id", true) == false)
					{
	$term["post"] = $term["post"];
						Database::Insert(CONF::$WPPrefex."term_relationships", array(
							"object_id"=>$term["post"],
							"term_taxonomy_id"=> (int)$termdata
						));
					}
					
				}
			}
		}
		return (($data_post_rows > 0 || count($data_post_rows) > 0 ) &&  (($data_terms == NULL || ($data_dataset_rows > 0 || count($data_dataset_rows) > 0 ) && ($data_terms > 0 || count($data_terms) > 0 ) && ($data_terms_rel > 0 || count($data_terms_rel) > 0 ) && ($data_terms_taxo > 0 || count($data_terms_taxo) > 0)))) ? $data_post_rows : FALSE;
	}
	
	private static function slug($string)
	{
		return str_replace(" ", "-", strtolower(trim(trim($string),"/")));
	}
	

	
	
}